<template>
 	<view class="Page">
 		<view class="title">库位物料列表</view>
 		<view class="noData" v-show="list.length==0">当前无物料数据</view>
 		<view v-show="list.length!=0" class="is_show_box">
 			<view>
 				<view class="data_item" v-for="(item ,i) in list" :key="i" @click="itemClick(i)">
					<view><text style="color: black;">({{i+1}})</text><text class="gray">名称：</text>{{item.spare_manage__name}}{{' '}}<text class="gray">库存：</text><text style="color: red;"> {{item.num}}</text>{{item.spare_manage__unit__name}}</view>
 				</view>
 			</view>	
 
 		</view>

		<uni-popup ref="detail">
		  <view class="card" v-if="list.length > index">
		  	<view><text class="gray">物料名称：</text>{{list[index].spare_manage__name}}</view>
			<view><text class="gray">物料编码：</text>{{list[index].spare_manage__code}} {{' '}}<text class="gray">初始数量：</text>{{list[index].init_num}}</view>
			<view style="font-weight:600 ;">库存数量({{list[index].spare_manage__unit__name}})：<text style="color: red;">{{list[index].num}} </text></view> 
			<view><text class="gray">库存属性：</text>{{list[index].now_spare_status__name}}{{' '}}<text class="gray">型号：</text>{{list[index].spare_manage__model}}</view>
			<view><text class="gray">规格：</text> {{list[index].spare_manage__specs}}{{' '}}<text class="gray">流水号：</text>{{list[index].no}}</view>
			<view><text class="gray">材质：</text>{{list[index].spare_manage__material}}</view>
			<view><text class="gray">库存位置：</text>{{list[index].store_place__flag}}</view>
			<view><text class="gray">仓库名称：</text>{{list[index].store_place__storage__name}}</view>
			<view><text class="gray">供应商：</text>{{list[index].spare_batch__company__name}}</view>
			<view><text class="gray">供货日期：</text>{{list[index].spare_batch__date}}</view>
				
		  </view>
		</uni-popup>	
		<uni-popup ref="notlocation" type="message">
			<uni-popup-message type="error" message="扫的码不是位置码" :duration="2000"></uni-popup-message>
		</uni-popup>
		<uni-popup ref="putGoalErr" type="message">
			<uni-popup-message type="error" message="找不到该库位,请选择正确的库位" :duration="2000"></uni-popup-message>
		</uni-popup>
		<view class="loadMoreBox" v-show="page_index<page_count">
			<button size="default" type="primary"  @click="loadMore">加载更多</button>
		</view>
 		<view class="kongbai">
 			
 		</view>
 	</view>
	<view class="input_goal"><label style="font-size: 14px;">选择库位：</label><input v-model="location" style="font-size: 14px;" /><image src="../../static/scan_icon.svg" mode="aspectFit" @click="scan_goal"></image></view>
	<view class="buttonBox">
		<button size="default" type="primary"  @click="panku">盘库</button>
	</view>
 </template>
 
 <script>
 	export default {
 		data() {
 			return {
 				qr_code:"",
 				list:[],

 				org_id:34,
				choosedID:'',
				location:'',
				if_scan:false,
				is_find:false,
				page_index:1,
				page_count:0,
				index:0,
 			}
 		},
		watch: {
		    location: {
		        handler(oldValue,newVal) {
					console.log(this.if_scan)
					if(this.if_scan){
						this.if_scan=false;
					}
					else{
						this.choosedID='';
					}
		        }
		    }
		},
 		onLoad() {
 			this.scan_goal();
 		},
 		methods: {
			itemClick(i){
				this.index=i;
				console.log('i'+this.index)
				this.$refs.detail.open('center')
			},
			scan_goal(){
				let that=this;
				uni.scanCode({
					scanType: ['qrCode'],
					success: function (res) {
						console.log(res.result)
						that.if_scan=true;
						let strArray=res.result.split(',')
						console.log(strArray)
						if(strArray[0][5]!='g'){
							// that.$refs.notlocation.open('center')
							that.$root.message('fail','扫的码不是位置码')
						}
						else{
							that.choosedID=strArray[0].slice(14);
							that.location=strArray[1].slice(5);
							console.log(that.choosedID);
							console.log('location'+that.location);
						}
				 
					},
				});
			},
			panku(){
				if(this.choosedID==''){
					let that=this;
					// dd.httpRequest({
					//   url: 'http://113.15.231.21:180/storage_manage2/ding_app/query_storage_place_info_list',
					//   method: 'POST',
					//   headers: {      sessionid:that.$root.sessionID },
					//   data: {
					// 	org_id:34, 
					//   },
					//   dataType: 'json',
					//   success: function(res) {
					// 	console.log("success")
					// 	// console.log(res)
					// 	// console.log('查寻目标库位')
					// 	console.log(res.data.result.list)
					// 	for(var item in res.data.result.list){
					// 		if(item.name==that.location){
					// 			that.choosedID=item.id;
					// 			that.is_find=true;
					// 		}
					// 	}
					// 	if(!that.is_find){
					// 		that.$refs.putGoalErr.open('center')
					// 	}
					// 	else{
					// 		// that.$refs.is_move.open('center')
					// 		console.log('盘库')
					// 		this.pankuConfirm();
					// 	}
						
					//   },
					//   fail: function(res) {
					// 	console.log("fail")
					//   },
					//   complete: function(res) {
					// 	console.log("com")
					//   }
					// });
					that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_storage_place_info_list',
						{
							org_id:34,
						}, 
						{
						  	sessionid:that.$root.sessionID
						})
						.then((res) => {
							let fun=function(){
								console.log("success")
								// console.log(res)
								// console.log('查寻目标库位')
								console.log(res.data.result.list)
								for(var item in res.data.result.list){
									// console.log(res.data.result.list[item].name);
									if(res.data.result.list[item].name==that.location){
										that.choosedID=res.data.result.list[item].id;
										that.is_find=true;
									}
								}
								if(!that.is_find){
									// that.$refs.putGoalErr.open('center')
									that.$root.message('fail','找不到该库位,请选择正确的库位')
								}
								else{
									// that.$refs.is_move.open('center')
									console.log('盘库')
									that.pankuConfirm();
									
								}
							}
							that.$root.judge(res,fun);
						})
				}
				else{
					// this.$refs.is_move.open('center')
					console.log('盘库')
					this.pankuConfirm();
				}
			},
			pankuConfirm(){
				this.list=[];
				let that=this;
				// dd.httpRequest({
				//   url: 'http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
				//   method: 'POST',
				//   headers: {      sessionid:that.$root.sessionID },
				//   data: {
				// 	org_id:34,               // 组织ID
				// 	store_place_id:that.choosedID,       // 目标库位
				// 	is_active:true,
				// 	page_size: 50,
				// 	page_index:1,

				//   },
				//   dataType: 'json',
				//   success: function(res){
				// 	that.page_count=res.data.result.page_count;
				// 	console.log('盘库数据')
				// 	console.log(res)
				// 	console.log(res.data.result.list)
				// 	that.list=res.data.result.list
				// 	},
				// 	fail: function(res) {
				// 		console.log("fail")
				// 	},
				// 	complete: function(res) {
				// 		console.log("com")
				// 	}
				// });
				that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
					{
						org_id:34,               // 组织ID
						store_place_id:that.choosedID,       // 目标库位
						is_active:true,
						page_size: 50,
						page_index:1,
					}, 
					{
					  	sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							that.page_count=res.data.result.page_count;
							console.log('盘库数据')
							console.log(res)
							console.log(res.data.result.list)
							that.list=res.data.result.list
							that.location='';
						}
						that.$root.judge(res,fun);
					})
			},
			loadMore(){
				
				let that=this;
				that.page_index+=1;
				console.log('page_count'+that.page_index)
				console.log(that.page_count>=that.page_index)
				// dd.httpRequest({
				//   url: 'http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
				//   method: 'POST',
				//   headers: {      sessionid:that.$root.sessionID },
				//   data: {
				// 	org_id:34,               // 组织ID
				// 	store_place_id:that.choosedID,       // 目标库位
				// 	is_active:true,
				// 	page_size: 50,
				// 	page_index:that.page_index,
				//   },
				//   dataType: 'json',
				//   success: function(res){
				// 	that.list.push(...res.data.result.list)
				// 	console.log('拼接数组')
				// 	console.log(that.list)
				// 	},
				// 	fail: function(res) {
				// 		console.log("fail")
				// 	},
				// 	complete: function(res) {
				// 		console.log("com")
				// 	}
				// });
				that.$root.http('http://113.15.231.21:180/storage_manage2/ding_app/query_spare_device_list',
					{
						org_id:34,               // 组织ID
						store_place_id:that.choosedID,       // 目标库位
						is_active:true,
						page_size: 50,
						page_index:that.page_index,
					}, 
					{
					  	sessionid:that.$root.sessionID
					})
					.then((res) => {
						let fun=function(){
							that.list.push(...res.data.result.list)
							console.log('拼接数组')
							console.log(that.list)
						}
						that.$root.judge(res,fun);
					})
			}
 		}
 	}
 </script>
 
 <style scoped>
 	.Page{
 		width: 100%;
 		font-size: 14px;
  		background-color: #F6F6F6;
 		display: flex;
 		flex-direction: column;
 		/* align-items: center; */
		padding-left: 10px;
		padding-right: 10px;
 		
 	}
 	.title{
 		font-size: 16px;
 		margin-top: 10px;
 		font-weight: 600;
		border-left: 2px solid dodgerblue;
		padding-left: 8px ;
 	}
 	.noData{
 		margin-top: 15px;
 	}
 	
 
 	.data_item{
 		margin-top: 12px;
 		width: 90%;
 		background-color: white;
 		border-radius: 5px;
 		font-size: 14px;
 		padding: 5px;
 	}
	.card{
		background-color: white;
		padding: 10px;
		border-radius: 10px;
	}
	.card view{
		margin-top: 5px;
	}
 	.gray{
 		color: #93A1A1;
 	}

	.dialog_center{
 		display: flex;
 		flex-direction: row;
 		justify-content: center;
 		align-items: center;
 	}
 	.is_show_box{
 		width: 100%;
 	}
	.input_goal{
		position: fixed;
		bottom: 49px;
		width: 100%;
		height: 40px;
		background-color: white;
		line-height: 40px;
		display: flex;
		justify-content: space-around;
		align-items: center;
	}
	.input_goal input{
		border: 1px solid gainsboro;
		width: 55%;
		height: 30px;
	}
	.input_goal image{
		width: 12%;
		height: 40px;

	}
	.loadMoreBox{
		width: 100%;
		margin-top: 12px;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
	}
	.loadMoreBox button{
		width: 30%;
	}
 	.kongbai{
 		width: 100%;
 		height: 100px;
 	}
 	.buttonBox{
 		width: 100%;
 		position: fixed;
 		bottom: 0%;
 		display: flex;
 		flex-direction: row;
 		/* justify-content: space-around; */
 		align-items: center;
 	}
 	.buttonBox button{
 		width: 100%;
 	}

 </style>
 